<template>
    <div class="user-container">
        <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
            <van-list class="user-list" v-model:loading="loading" :finished="page >= lastPage" finished-text="没有更多了"
                @load="onLoadMore">
                <luckListcode @change-status="onChangeStatus" @clickControl="clickControl"
                    v-for="user in luckDrawsLists" :key="user.id" :user="user" />
            </van-list>
            <van-back-top bottom="10vh" />
        </van-pull-refresh>
    </div>
</template>
<script setup>
import { luckDrawCodes } from '@/api/lucky/index'
import { showDialog, showNotify, showToast } from 'vant'
import luckListcode from '@/components/luckListcode.vue'
import { onMounted, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
const refreshing = ref(false)
const page = ref(1)
const lastPage = ref(1)
const loading = ref(false)
const luckDrawsLists = ref([])
const luckDrawId = ref('')


const route = useRoute();

onMounted(() => {
    refreshing.value = true
    luckDrawId.vlaue = route.query.id;
    getluckDrawCodes()
})
const onRefresh = () => {
    page.value = 1
    lastPage.value = 1
    refreshing.value = true
    getluckDrawCodes()
}
const getluckDrawCodes = async () => {
    try {
        const result = await luckDrawCodes({
            page: page.value,
            status: '',
            limit: 10000,
            level: '',
            userId: '',
            startTime: '',
            endTime: '',
            luckDrawId:luckDrawId.vlaue
        })
        if (result.code === 200) {
            luckDrawsLists.value = []
            if (page.value === 1) {
                luckDrawsLists.value = result.data.data
            } else {
                luckDrawsLists.value = luckDrawsLists.value.concat(result.data.data)
            }
            page.value++
            lastPage.value = result.data.last_page
        }

    } catch (e) {
        console.log(e)
    } finally {
        loading.value = false
        refreshing.value = false
    }
}
const onLoadMore = () => {
    if (page.value >= lastPage.value) return
}
const clickControl = (e) => {
    console.log(e)
    if (e.status == 2) {
        showNotify({
            type: 'danger',
            message: '随机码已经使用'
        })
    } else {
        navigator.clipboard.writeText(e.code).then(() => {
            showNotify({
                type: 'success',
                message: '复制成功'
            })
        },
            (error) => {

                showNotify({
                    type: 'danger',
                    message: '复制失败'
                })
            }
        );
    }

}

</script>
<style scoped lang="less"></style>